<?php

namespace App\Http\Controllers\Api\V1;
use App\Models\DeepNew;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Api\V1\Resources\NewsListResource;
use App\Http\Controllers\Api\V1\Resources\NewsResource;
use Illuminate\Database\Eloquent\Builder;
use App\Supports\LinkedIn\Client;
class DeepNewsController extends Controller
{
	public function index()
	{


		$news = DeepNew::query()->where('is_examine',true)->orderByDesc("id")->paginate(10);
	
				
		return NewsListResource::collection($news);
	}


	public function show($id)
	{
		$new = DeepNew::find($id);
		return new NewsResource($new);
	}


	public function shareToLinkdin($id)
	{

		$new = DeepNew::find($id);
		if(!$new){

			fail('当前新闻不存在');
		}
		$user = request()->user();
		$client = new Client(
		    getenv('LINKDIN_CLIENT_ID'),
		    getenv('LINKDIN_CLIENT_SECRET'),
		);
		$client->setRedirectUrl('https://www.xbreakingtechinsight.com');
		
		$client->setAccessToken($user->linkdin_token);
		$arr = [                                   
                    'author' => 'urn:li:person:' . $user['nickname'],
                    'lifecycleState' => 'PUBLISHED',
                    'specificContent' => [          
                        'com.linkedin.ugc.ShareContent' => [
                            'shareCommentary' => [
                                'text' => $new->title_zn
                            ],
                            'shareMediaCategory' => 'ARTICLE',
                            'media' => [
                                [
                                    'status' => 'READY',
                                    'description' => [
                                        'text' => strip_tags($new->brief_zn)
                                    ],
                                    'thumbnails'=> [
                                    	['url' => img_path($new->images)]
                                    	
                                    ],

                                    //img_path($new['images']
                                    'originalUrl' => "https://www.xbreakingtechinsight.com/detail/".$new->id,
                                    'title' => [
                                        'text' => $new->title_zn
                                    ]
                                ]
                            ]
                        ]
                    ],
                    'visibility' => [
                        'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
                    ]
                ];

               //var_dump($arr); die;
		$share = $client->post(                 
                'ugcPosts',                         
                $arr
            );
		
		return response()->json(['code' => 200]);
	}
	
}
